clear all
set more off, perm
local mydir "`1'"
global Tables 	`mydir'/TS/Tables
global Work   	`mydir'/TS/DataWork
global Raw    	`mydir'/TS/DataRaw
global WRDS  	`mydir'/WRDS
/* ********************************************************************* */
* DEAL WITH PRICE TARGET DATA ONLY
* [#3A] load price targets from IBES 
import sas using $WRDS/IBES/ptgsumu.sas7bdat, case(lower) clear
keep if (index(measure,"PTG")>0 & usfirm==1 & index(curr,"USD")>0) 

replace ticker=trim(ticker)
keep ticker oftic cname cusip statpers medptg   
recast str cname
compress

keep if medptg<.
* ****************************************
* ****************************************
merge m:1 ticker statpers using $Raw/ibes01_brown_2019, keepusing(permco permno ticker statpers)
drop if _merge==2
drop _merge
merge m:1 ticker statpers using $Raw/link_ibes_crsp2022, keepusing(permco permno ticker statpers) update
drop if _merge==2 | permco==.
drop _merge
* ****************************************
* ****************************************
* restrict IBES sample to members of S&P 
tempfile tmp
save "`tmp'", replace

import sas using $WRDS/CRSP/dsp500list.sas7bdat, clear case(lower)
joinby permno using "`tmp'"
keep if statpers>=start & statpers<=ending
drop start ending
count
* ****************************************
save $Work/cs01_ptg, replace
* ****************************************